package com.springboot.kaoqing.controller;


import com.springboot.kaoqing.entity.Kecheng;
import com.springboot.kaoqing.entity.SysUser;
import com.springboot.kaoqing.qpage.QPage;
import com.springboot.kaoqing.service.IKechengService;
import com.springboot.kaoqing.service.ISysUserService;
import com.springboot.kaoqing.util.QRCodeUtil;
import com.springboot.kaoqing.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Random;

/**
 * <p>
 * 课程表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-11-06
 */
@Controller
@RequestMapping("/api/kecheng")
public class KechengController {

    @Autowired
    private ISysUserService iUsersService;

    @Autowired
    private IKechengService service;

    /**
     * 路由地址
     */
    public String prefix = "/view/admin/kecheng";


    /**
     * 进入管理页面
     * @return
     */
    @RequestMapping("/index")
    public String index(){
        return prefix+"/index";
    }

    /**
     * 进入教师页面
     * @return
     */
    @RequestMapping("/jiaoshi/index")
    public String jiaoshiindex(){
        return "/view/admin/jiaoshi/index";
    }

    /**
     * 进入学生页面
     * @return
     */
    @RequestMapping("/xuesheng/index")
    public String xueshengindex(){
        return "/view/admin/xuesheng/index";
    }

    /**
     * 获取所有的列表
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Result getList(QPage qPage, Kecheng obj, HttpServletRequest request) {
        SysUser sysUser=(SysUser) request.getSession().getAttribute("user");
        if(sysUser.getType()==2){//教师查看自己的课程
            obj.setUid(sysUser.getId());
        }
        return Result.reOk("查询成功",this.service.getPageList(qPage,obj));
    }


    /**
     * 进入新增页面
     * @return
     */
    @RequestMapping("/add")
    public String add(){
        return prefix+"/add";
    }

    /**
     * 新增保存
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public Result insert(Kecheng obj) {
        SysUser sysUser=this.iUsersService.getById(obj.getUid());
        obj.setUname(sysUser.getNickname());
        obj.setCtime(new Date());
        return this.service.insert(obj);
    }

    /**
     * 进入编辑页面
     * @return
     */
    @RequestMapping("/edit")
    public String edit(){
        return prefix+"/edit";
    }

    /**
     * 根据ID获取信息
     * @param id
     * @return
     */
    @RequestMapping("/getById")
    @ResponseBody
    public Result getById(Integer id) {
        Kecheng kecheng=this.service.getById(id);
        return Result.reOk("查询成功",kecheng);
    }

    /**
     * 编辑保存
     * @return
     */
    @RequestMapping("/updates")
    @ResponseBody
    public Result updates(Kecheng obj) {
        obj.setUtime(new Date());
        return this.service.updates(obj);
    }


    /**
     * 删除保存
     * @return
     */
    @RequestMapping("/deleteId")
    @ResponseBody
    public Result deleteId(int id) {
        boolean isOk=this.service.removeById(id);
        if(isOk){
            return Result.reOk("删除成功");
        }
        return Result.reError("删除失败");
    }


    /**
     * 生成二维码
     * @return
     */
    @RequestMapping("/qrcode")
    @ResponseBody
    public Result qrcode(int id) {
        Kecheng kecheng=this.service.getById(id);
        if(kecheng.getQrcodenum()!=null&&!kecheng.getQrcodenum().equals("")){
            return Result.reError("二维码已生成过，无法重新生成");
        }
        //获取随机数字
        Random random = new Random();
        // 生成一个6位的随机数
        int randomNumber = 100000 + random.nextInt(900000); // 100000到999999之间
        String imgName =kecheng.getKno() + ".png";
        String qrCodePath ="uploads/"+ imgName;
        //生成二维码到服务器路径
        QRCodeUtil.save(randomNumber+"", null, "D://javaimage//"+qrCodePath);
        kecheng.setQrcodenum(randomNumber+"");
        kecheng.setQrcode(qrCodePath);
        kecheng.setUtime(new Date());
        boolean isOk=this.service.updateById(kecheng);
        if(isOk){
            return Result.reOk("生成成功，二维码签到随机数位："+randomNumber);
        }
        return Result.reError("生成失败");
    }




}
